package com.java.admin.spareinventory.controller;

import com.github.pagehelper.PageInfo;
import com.java.admin.spareinventory.service.SpareinventoryService;
import com.java.database.entity.Spareinventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 库存表业务逻辑层
 */
@Controller
public class SpareinventoryController {

    @Autowired
    private SpareinventoryService spareinventoryService;

    /**
     * 添加库存表数据
     * @return
     */
    @RequestMapping("/addSpareinventory")
    public String addSpareinventory(@RequestParam("fileName") MultipartFile fileName, Spareinventory spareinventory, HttpServletRequest request)throws IOException {

        try {
            //调用上传文件方法
            uploadingFile(fileName);
            //添加库存表数据数据
            spareinventory.setSpareinventoryicon(fileName.getOriginalFilename());
            spareinventoryService.addSpareinventory(spareinventory);
        } catch (Exception e) {
            throw new RuntimeException("添加失败请联系管理员:1544");
        }
        return "redirect:/admin/spareinventory.html";
    }

    /**
     * 模糊分页查询
     * @return
     */
    @RequestMapping("/selectAll")
    @ResponseBody
    public PageInfo selectAll(String pageNum, String condition){

        PageInfo pageInfo = spareinventoryService.selectAll(pageNum,condition);
        return pageInfo;
    }

    /**
     * 配件下架
     * @param spareInventoryId
     * @return
     */
    @RequestMapping("/delecteById")
    public String delecteById(String  spareInventoryId){
        try{
            spareinventoryService.delecteById(spareInventoryId);
        }catch (Exception e){
            throw new RuntimeException("下架失败请联系管理员:4541");
        }
        return "redirect:/admin/outofstock.html";
    }

    /**
     * 通过id查询
     * @param spareInventoryId
     * @return
     */
    @RequestMapping("/selectById")
    @ResponseBody
    public Spareinventory selectById(String spareInventoryId){
        Spareinventory spareinventory = spareinventoryService.selectById(spareInventoryId);
        return spareinventory;
    }

    /**
     * 修改库存表
     * @param fileName
     * @param spareinventory
     * @return
     */
    @RequestMapping("/updateByPrimaryKey")
    public String updateByPrimaryKey(@RequestParam("fileName") MultipartFile fileName,Spareinventory spareinventory){
        try{
            //调用上传文件方法
            uploadingFile(fileName);
            //修改数据
            spareinventory.setSpareinventoryicon(fileName.getOriginalFilename());
            spareinventoryService.updateByPrimaryKey(spareinventory);
        }catch (Exception e){
            throw new RuntimeException("修改失败请联系管理员:4524");
        }
        return "redirect:/admin/spareinventory.html";
    }

    /**
     * 模糊查询下架
     * @param pageNum
     * @param condition
     * @return
     */
    @RequestMapping("/selectOutOfStock")
    @ResponseBody
    public PageInfo selectOutOfStock(String pageNum, String condition){
        PageInfo pageInfo = spareinventoryService.selectOutOfStock(pageNum,condition);
        return pageInfo;
    }

    /**
     * 通过id删除
     * @param spareInventoryId
     * @return
     */
    @RequestMapping("/deleteByPrimaryKey")
    public String deleteByPrimaryKey(String spareInventoryId){
        spareinventoryService.deleteByPrimaryKey(spareInventoryId);
        return "redirect:/admin/outofstock.html";
    }

    /**
     * 配件上架
     * @param spareInventoryId
     * @return
     */
    @RequestMapping("/upateAdded")
    public String upateAdded(String spareInventoryId){
        spareinventoryService.upateAdded(spareInventoryId);
        return "redirect:/admin/spareinventory.html";
    }

    /**
     * 上传文件方法
     */
    public void uploadingFile(MultipartFile multipartFile)throws IllegalStateException, IOException{
        String name = multipartFile.getName();
        String originalName = multipartFile.getOriginalFilename();
        multipartFile.transferTo(new File("D:/"+originalName));
    }

}

